package com.uwntek.base.xmlToBean;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.uwntek.base.XmlBean;
import com.uwntek.base.des.DesSecurity;
import com.uwntek.pojo.Projectinfo;
import com.uwntek.pojo.Userman;
import com.uwntek.service.IProjectService;
import com.uwntek.service.Imp.ProjectServiceImpl;

public class XmlToBean {

	public void xmlToBean(String xml) {
		XStream xstream = new XStream();
		xstream.alias("userman", Userman.class);
		xstream.alias("project", Projectinfo.class);
		XmlBean bean = (XmlBean) xstream.fromXML(xml);
		Projectinfo project = bean.getProject();
		
	//	 DesEncrypt des=new DesEncrypt();
		
		//	des.getKey("UWNTEK_CLIENT");
		//	String strEnc = des.getEncString(xml);
			
			try {
				DesSecurity des = new DesSecurity("UWNTEK_CLIENT", "12345678");
				project.setProjectId(des.encrypt(project.getProjectId()));
				project.setProjectName(des.encrypt(project.getProjectName()));
				project.setUrl(des.encrypt(project.getUrl()));
				project.setVersion(des.encrypt(project.getVersion()));
				project.setComment(des.encrypt(project.getComment()));
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			
//			project.setVar1(des.getEncString(project.getVar1()));
//			project.setVar2(des.getEncString(project.getVar2()));
//			project.setVar3(des.getEncString(project.getVar3()));
//			project.setVar4(des.getEncString(project.getVar4()));
//			project.setVar5(des.getEncString(project.getVar5()));
//		IProjectService pro = new ProjectServiceImpl();
//		pro.create(project);
			boolean flag = false;
			IProjectService projectService = new ProjectServiceImpl(); 
			try {
				String sql = "From Projectinfo as a where a.projectId='"+project.getProjectId()+"'";
				List list = projectService.queryProjectInfo(sql);
				if(list==null||list.size()==0)
				 {
				     flag = projectService.saveProjectInfo(project);
					 if(flag =true){
							System.out.println("save success");
						}
				 }
				else{
					project.setId(((Projectinfo)list.get(0)).getId());
					flag =projectService.updateProjectInfo(project);
					
					if(flag =true){
						System.out.println("update success");}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			
			}
	}
}
